package edu.scu.declaresys.service.impl;

import java.util.Date;

import edu.scu.declaresys.config.Configs;
import edu.scu.declaresys.dao.DAOFactory;
import edu.scu.declaresys.dao.intr.INewsDAO;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.exception.ExecuteException;
import edu.scu.declaresys.service.intr.INewsService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.News;
import edu.scu.declaresys.vo.NewsClass;

/**
 * 
 * @author: zt
 * @since : 2013-7-8
 */
public class NewsServiceImpl implements INewsService
{
	private INewsDAO newsDao = DAOFactory.createNewsDAO();
	
	//
	// find list.
	//
	@Override
	public PageList<News> findNewsList(Integer newsClassId, Integer pageNum)
			throws ExecuteException
	{
		return newsDao.findByProperty(News.T_NEWS_CLASS_ID, newsClassId, Depth.ONE,
				pageNum, Configs.NEW_AMT_PER_PAGE,
				News.T_ID, true);
	}
	
	@Override
	public PageList<News> findLatestBulletinNews() throws ExecuteException
	{
		return newsDao.findByProperty(News.T_NEWS_CLASS_ID, NewsClass.BULLETIN_BOARD_ID, Depth.ZERO,
				1, NewsClass.BULLETIN_BOARD_NEWS_SIZE,
				News.T_PUBLISH_TIME, false);
	}
	
	@Override
	public PageList<News> findLatestFundNews() throws ExecuteException
	{
		return newsDao.findByProperty(News.T_NEWS_CLASS_ID, NewsClass.FUND_NEWS_ID, Depth.ZERO,
				1, NewsClass.FUND_NEWS_SIZE,
				News.T_PUBLISH_TIME, false);
	}
	
	@Override
	public PageList<News> findLatestProjectNews() throws ExecuteException
	{
		return newsDao.findByProperty(News.T_NEWS_CLASS_ID, NewsClass.PROJECT_NEWS_ID, Depth.ZERO,
				1, NewsClass.PROJECT_NEWS_SIZE,
				News.T_PUBLISH_TIME, false);
	}
	
	@Override
	public PageList<News> findLatestDeclareProcessNews() throws ExecuteException
	{
		return newsDao.findByProperty(News.T_NEWS_CLASS_ID, NewsClass.DECLARE_PROCESS_ID, Depth.ZERO,
				1, NewsClass.DECLARE_PROCESS_SIZE,
				News.T_PUBLISH_TIME, false);
	}
	
	@Override
	public News findNewsById(Integer id) throws ExecuteException
	{
		return newsDao.findById(id, Depth.ONE);
	}
	
	@Override
	public News findByTitle(String title) throws ExecuteException
	{
		return newsDao.findByPropertyUnique(News.T_TITLE, title, Depth.ONE);
	}
	//
	// write method.
	//

	@Override
	public News addNews(News news) throws ExecuteException
	{
		news.setPublishTime(new Date());
		return newsDao.findById(newsDao.save(news), Depth.ZERO);
	}

	@Override
	public void delete(Integer id) throws ExecuteException
	{
		newsDao.delete(id);
		return ;
	}
}
